<!DOCTYPE html>
<html lang="zh-cn" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <meta name="renderer" content="webkit">
    <title></title>
    <link rel="stylesheet" href="css/pintuer.css">
    <link rel="stylesheet" href="css/admin.css">
    <script src="js/jquery.js"></script>
    <script src="js/pintuer.js"></script>
    <script src="/js/vuejs-2.5.16.js"></script>
    <script src="/js/axios-0.18.0.js"></script>
</head>
<body>
<div class="panel admin-panel" id="categoryDiv">
    <div class="panel-head" id="add"><strong><span class="icon-pencil-square-o"></span>添加问题</strong></div>
    <div class="body-content">
        <div class="form-x">
<!--            <input type="hidden" name="id" v-model="question.id">-->

            <div class="form-group">
                <div>
                    <label style="font-weight: bolder">问题内容：</label><input v-model="question" style="width: 500px"/>
                </div>

                <!--        <div class="field">-->
                <!--          <input type="text" class="input w50" v-model="category.categoryName" name="categoryName" data-validate="required:请输入名称" />-->
                <!--          <div class="tips"></div>-->
                <!--        </div>-->
            </div>
            <div class="clear"></div>
            <div v-for="i of 7" style="font-size: larger">
                选项内容:<input v-model="options[i-1].options"/>
                选项分数:<input v-model="options[i-1].score"/>
                <input type="hidden" name="id" v-model="options[i-1].id"/>
            </div>

            <!--            <button @click="add"></button>-->


            <!--      <div class="form-group">-->
            <!--        <div class="label">-->
            <!--          <label>排序：</label>-->
            <!--        </div>-->
            <!--        <div class="field">-->
            <!--          <input type="number" class="input w50" name="orderNo"    v-model="category.orderNo" data-validate="number:排序必须为数字" />-->
            <!--          <div class="tips"></div>-->

            <!--        </div>-->
            <!--      </div>-->

            <div class="form-group">
                <div class="label">
                    <label></label>
                </div>
                <div class="field">
                    <button class="button bg-main icon-check-square-o" @click="save()"> 提交</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    const app = new Vue({
        el: '#categoryDiv',
        data: {
            // soulQuestion: {},
            question: "",
            options: [
                {
                    options: "",
                    score: "0",
                    id:""
                }, {
                    options: "",
                    score: "0",
                    id:""
                }, {
                    options: "",
                    score: "0",
                    id:""
                }, {
                    options: "",
                    score: "0",
                    id:""
                }, {
                    options: "",
                    score: "0",
                    id:""
                }, {
                    options: "",
                    score: "0",
                    id:""
                }, {
                    options: "",
                    score: "0",
                    id:""
                },
            ],
            soulTestId: "",
            soulQuestionId:""
        },
        methods: {
            findCategoryById(id) {
                axios.get('/management/soulQuestionById?id=' + id)
                    .then(resp => {
                        this.question = resp.data.question;
                        let li = resp.data.options
                        for (let i = 0; i < li.length; i++) {
                            this.options[i] = li[i]
                        }
                    })
            },
            save() {
                if (this.soulTestId.length) {
                    let list =[];
                    for (let i = 0; i < this.options.length; i++) {
                        if (this.options[i].options != "") {
                            list.push({
                                options: this.options[i].options,
                                score: this.options[i].score,
                            })
                        }
                    }
                    let params = {
                        id: this.soulTestId,
                        question: this.question,
                        options: list

                    };
                    axios.post("/management/soulQuestion",params).then(resp => {
					console.log(params);
                        location.href = "/question.html?soulTestId="+this.soulTestId;
                    });
                } else  {
                    let list =[];
                    for (let i = 0; i < this.options.length; i++) {
                        if (this.options[i].options != "") {
                            list.push({
                                options: this.options[i].options,
                                score: this.options[i].score,
                                id: this.options[i].id
                            })
                        }
                    }
                    let params = {
                        id: this.soulQuestionId,
                        question: this.question,
                        options: list
                    };
                    axios.put("/management/soulQuestion",params
                    ).then(resp => {
                        location.href = "/question.html?/management/soulTestId="+resp.data;
                    });
                }
            }

        },
        created() {
            if (location.search.split("?")[1].split("=")[0] == "soulQuestionId") {
                let id = location.search.split("?")[1].split("=")[1];
                this.soulQuestionId=id;
                this.findCategoryById(id)
            } else {
                this.soulTestId = location.search.split("?")[1].split("=")[1];
                console.log(this.soulTestId)
            }
        }
    })
</script>
</body>
</html>